<?php
class Slidetype extends Ot_Db_Table
{   
    protected $_name = 'tbl_slidetype';
    
    protected $_primary = 'slidetype';
    
    public function getEnabledTypes()
    {
        $where = $this->getAdapter()->quoteInto('status = ?', 'enabled');
        
        $results = $this->fetchAll($where);
        
        if ($results->count() == 0) {
            return array();
        }
        
        $returnData = array();
        foreach ($results as $r) {
            $returnData[] = array(
                'name'      => $r->slidetype,
                'object'    => $this->factory($r->slidetype),
            );
        }
        
        return $returnData;        
    }
    
    public static function factory($slidetype)
    {           
        $classname = 'Slidetype_' . $slidetype;
        
        set_include_path(get_include_path() . PATH_SEPARATOR . self::getSlidetypePath($slidetype));
        
        /*
         * Load the adapter class.  This throws an exception
         * if the specified class cannot be loaded.
         */
        require_once($slidetype . '.php');
        
        $adapter = new $classname();
        
        /*
         * Verify that the object created is a descendent of the abstract adapter type.
         */
        if (!$adapter instanceof Bbce_Slidetype_Abstract) {
            throw new Exception("Plugin class '$classname' does not extend Bbce_Slidetype_Abstract");
        }

        return $adapter;
    }
    
    /**
     * gets the basepath to the plugin
     * 
     * @param $plugin
     * @return string
     */
    public static function getSlidetypePath($slidetype = '')
    {
        return realpath(APPLICATION_PATH . '/../loadable/slidetype/' . $slidetype);
    }
}